package hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class DAOStation 
{
	private Session session = null;
	// Question 1
	public DTOStationList getMostChangedVersion() {	
		DTOStationList dtoStationList = new DTOStationList();
		try {
			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			session = sessionFactory.openSession();
			String q = "from Station station where station.version = (select max(station.version) from Station station)";
			Query query = session.createQuery(q);
			List<Station> list = query.list();
			dtoStationList.setStationList(list);
			session.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
		}
		return dtoStationList;
	}
	// Question 2
	public DTOStationList getStoppedStation() 
	{
		DTOStationList dtoStationList = new DTOStationList();
		try {
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
			String q = "from Station station where station.isOpen = 0";
			Query query = session.createQuery(q);
			List<Station> list = query.list();
			dtoStationList.setStationList(list);
			session.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
		}
		return dtoStationList;
	}		
	
}
